package dao;

import pojo.Dept;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DeptDaoImpl {
    // psvm

    public List<Dept> findAll() {
        // 创建一个集合
        List<Dept> depts = new ArrayList<>();
        // jdbc
        //1、加载驱动
        try {
            Class.forName("com.mysql.jdbc.Driver");

            // 2、创建连接
            String url = "jdbc:mysql://localhost:3306/yggl?useUnicode=true&characterEncoding=utf8";
            // ctrl+d 复制一行
            String user = "root";
            String pass = "root";
            Connection connection = DriverManager.getConnection(url,user,pass);
            if (connection != null) {
                // sout
                System.out.println("连接成功");
            }
            // 3、定义sql语句
            String sql = "select * from departments;";
            // 4、预编译sql
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            // 5、执行查询
            ResultSet resultSet = preparedStatement.executeQuery();
            // 6、遍历结果
            while (resultSet.next()) {
                String id = (String) resultSet.getObject(1);
                String name = (String) resultSet.getObject(2);
                // ctrl+d
                System.out.println(id+"-----"+name);
            }
            return null;

        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
